var loading = false;
var app = new Vue({
    el: '#app',
    data: {
        type: [{
            name: '最新资讯',
            type: 1
        }, {
            name: '推荐资讯',
            type: 2
        }],
        types: 1,
        isShow: true,
        tabs: [{
            name: '企业动态',
            id: 1
        }, {
            name: '本地新闻',
            id: 2
        }, {
            name: '协会动态',
            id: 3
        }, {
            name: '政策动态',
            id: 4
        }, {
            name: '本地人物',
            id: 5
        }, {
            name: '企业访谈',
            id: 6
        }, {
            name: '评析',
            id: 7
        }, ],
        listTab: [],
        num: 0,
        bannerImg: [],
        bannerContent: [],
        Type: 1,
        pageIndex: 1,
        pageIndex2: 1,
        pageSize: 10,
        hideing: true,
        userId: 0,
        topStatus: false,
        parentData: [],
        sonData: [[]],
        createTime: '',
        headImg: '',
        headTxt: '',
        cityId:0,
    },
    created() {
        FastClick.attach(document.body);
        $(document.body).infinite();
        if ($.getPara("userId") != "") {
            var userId = $.getPara("userId");
            this.userId = userId;
            $.setStorage("userId", userId)
        } else {
            if ($.getStorage("userId") && $.getStorage("userId") != "") {
                var userId = parseInt($.getStorage("userId"));
                this.userId = userId
            }
        }
        if ($.getPara('cityId')) {  //获取分享的参数
            that.cityId = $.getPara('cityId');
        } 
        this.getBanner();
        this.getData()
    },
    mounted() {
        var that = this;
        window.addEventListener('scroll', function() {
            let scroll = $(document).scrollTop();
            if (scroll > 154) {
                that.topStatus = true;
                $(".swiper-wrapper").addClass("top-wip")
            } else {
                $("#tabs").removeClass("top-add");
                that.topStatus = false;
                $(".swiper-wrapper").removeClass("top-wip")
            }
        });
        $(document).on('touchmove', function(e) {
            let scroll = $(document).scrollTop();
            if (scroll > 154) {
                $("#tabs").addClass("top-add");
                $(".swiper-wrapper").addClass("top-wip")
            } else {
                $("#tabs").removeClass("top-add");
                $(".swiper-wrapper").removeClass("top-wip")
            }
        });
        var that = this;
        this.scrollMore()
    },
    methods: {
        getBanner() {
            var _this = this;
            var Type = 0;
            if (_this.types == 1) {
                Type = 7
            } else if (_this.types == 2) {
                Type = 8
            }
            $.ajax({
                url: "/api/banner-ad/banner/index",
                type: 'GET',
                async: true,
                data: {
                    type: Type,
                },
                timeout: 5000,
                dataType: 'json',
                success: function(res, textStatus, jqXHR) {
                    // console.log(res)
                    if (res.code == 200) {
                        _this.bannerImg = res.data;
                        _this.bannerImg.forEach(function(val, index) {
                            if (val.url_type == 0) {
                                _this.bannerContent += '<div class="swiper-slide"><div class="bannerimg_size"><img src="' + val.img + '" alt=""></div></div>'
                            } else {
                                _this.bannerContent += '<div class="swiper-slide"><div class="bannerimg_size"><img  onclick="BannerurlHref(' + val.url_value + ',' + val.url_type + ',' + _this.types + ','+val.city_id+')"src="' + val.img + '" alt=""></div></div>'
                            }
                        });
                        $("#newSwiper").html(_this.bannerContent)
                        _this.$nextTick(function(){
                            var swiper = new Swiper('.swiper-container',{
                                autoplay: 3000,
                                loop: true,
                                pagination: '.swiper-pagination',
                                autoplayDisableOnInteraction: false,
                                observer: true,
                                observeParents: true,
                            });
                        });
            
                    } else {
                        $.toast(res.msg, "text")
                    }
                },
                error: function(xhr, textStatus) {
                    $.toast(res.msg, "text")
                },
            })
        },
        getData() {
            var _this = this;
            $.ajax({
                url: "/api/information/latest",
                type: 'GET',
                async: true,
                data: {
                    type: _this.Type,
                    pageIndex: _this.pageIndex,
                    pageSize: _this.pageSize
                },
                timeout: 5000,
                dataType: 'json',
                success: function(res, textStatus, jqXHR) {
                    if (res.code == 200) {
                        var list = res.data.list;
                        if (list.length > 0) {
                            var original = _this.listTab;
                            var array = original.concat(list);
                            _this.listTab = array
                        } else {
                            loading = true
                        }
                        _this.hideing = true
                    } else {
                        $.toast(res.msg, "text")
                    }
                },
                error: function(xhr, textStatus) {
                    $.toast(res.msg, "text")
                },
                complete: function() {}
            })
        },
        getRecomData() {
            var _this = this;
            $.ajax({
                url: "/api/information/recommend",
                type: 'GET',
                async: true,
                data: {
                    pageIndex: _this.pageIndex2,
                },
                timeout: 5000,
                dataType: 'json',
                success: function(res, textStatus, jqXHR) {
                    if (res.code == 200) {
                        var list = res.data.list;
                        if (list.length > 0) {
                            var original = _this.parentData;
                            var array = original.concat(list);
                            _this.parentData = array
                        } else {
                            loading = true;
                            $.toast("没有更多数据了~", "text")
                        }
                        _this.hideing = true
                    } else {
                        $.toast(res.msg, "text")
                    }
                },
                error: function(xhr, textStatus) {
                    $.toast("请求失败，请重试", "text")
                },
                complete: function() {}
            })
        },
        tab(type) {
            if (type != this.Type) {
                this.Type = type;
                this.pageIndex = 1;
                this.listTab = [];
                this.hideing = false;
                this.getData()
            }
        },
        tabChange(type) {
            $("#tabs").removeClass("top-add");
            $(".swiper-wrapper").removeClass("top-wip");
            if (type != this.types) {
                this.listTab = [];
                this.types = type;
                if (this.types == 1) {
                    this.isShow = true;
                    this.pageIndex = 1;
                    this.getData();
                    this.bannerContent = [];
                    this.getBanner();
                    loading = false
                } else {
                    this.pageIndex = 2;
                    this.isShow = false;
                    this.getRecomData();
                    this.bannerContent = [];
                    this.getBanner()
                }
            }
        },
        scrollMore() {
            var that = this;
            this.$nextTick(function() {
                $(document.body).infinite().on("infinite", function() {
                    if (loading)
                        return;
                    loading = true;
                    that.hideing = false;
                    setTimeout(function() {
                        if (that.types == 1) {
                            var pageIndex = that.pageIndex;
                            pageIndex++;
                            that.pageIndex = pageIndex;
                            that.getData()
                        } else {
                            var pageIndex2 = that.pageIndex2;
                            pageIndex2++;
                            that.pageIndex2 = pageIndex2;
                            that.getRecomData()
                        }
                        loading = false
                    }, 1500)
                });
                that.swiperInit()
            })
        },
        // 最新资讯跳转
        urlHref(item) {
            console.log(item,"当前对象")
            var para = {};
            var that = this;
            var cityId = item.city_id;//分站id
            detailType(para);
            para.type = "newsDetails";
            if ($.getPara("userId") != "") {
                if (this.Type == 5 || this.Type == 6 || this.Type == 7) {
                    para.url = '/PageClassify/html/Substation/Character/subCharacterDetails.html?id=' + item.id + '&types=' + this.Type + '&userId=' + this.userId+"&cityId="+cityId;
                } else {
                    para.url = '/PageClassify/html/Substation/Dynamic/subDynamicDetails.html?id=' + item.id + '&types=' + this.Type + '&userId=' + this.userId+"&cityId="+cityId;
                }
            } else {
                if (this.Type == 5 || this.Type == 6 || this.Type == 7) {
                    para.url = '/PageClassify/html/Substation/Character/subCharacterDetails.html?id=' + item.id + '&types=' + this.Type + '&userId=0'+"&cityId="+cityId;
                } else {
                    para.url = '/PageClassify/html/Substation/Dynamic/subDynamicDetails.html?id=' + item.id + '&types=' + this.Type + '&userId=0'+"&cityId="+cityId;
                }
            }
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appPush.postMessage(para)
            } else {
                window.location.href = para.url;
                alert("PC浏览器")
            }
        },
        urlHref_li(item) {
            var cityId = item.city_id//city_id
            var type = item.type;//
            var id = item.object_id;
            console.log('urlHref_li')
            var para = {};
            detailType(para);
            para.type = "newsDetails";
            this.Type = type;
            if (this.Type == 5 || this.Type == 6 || this.Type == 7) {
                para.url = '/PageClassify/html/Substation/Character/subCharacterDetails.html?id=' + id + '&types=' + this.Type+"&cityId="+cityId;
            } else {
                para.url = '/PageClassify/html/Substation/Dynamic/subDynamicDetails.html?id=' + id + '&types=' + this.Type + '&userId=' + this.userId+"&cityId="+cityId;
            }
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appPush.postMessage(para)
            } else {
                window.location.href = para.url;
                alert("PC浏览器")
            }
        },
        goHeadUrl(item) {
            console.log('goHeadUrl',item)//
            var cityId = item.city_id//city_id
            var type = item.type;//
            var id = item.object_id;
            var para = {};
            detailType(para);
            para.type = "newsDetails";
            this.Type = type;
            if (this.Type == 5 || this.Type == 6 || this.Type == 7) {
                para.url = '/PageClassify/html/Substation/Character/subCharacterDetails.html?id=' + id + '&types=' + this.Type+"&cityId="+cityId;
            } else {
                para.url = '/PageClassify/html/Substation/Dynamic/subDynamicDetails.html?id=' + id + '&types=' + this.Type + '&userId=' + this.userId+"&cityId="+cityId;
            }
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appPush.postMessage(para)
            } else {
                window.location.href = para.url;
                alert("PC浏览器")
            }
        },
        
        // 轮播图配置
        swiperInit() {
            // var swiper = new Swiper('.swiper-container',{
            //     autoplay: 3000,
            //     loop: true,
            //     pagination: '.swiper-pagination',
            //     autoplayDisableOnInteraction: false,
            //     observer: true,
            //     observeParents: true,
            // });
            var myTabs = new Swiper('.swiper-tabs',{
                autoplay: 0,
                loop: false,
                autoplayDisableOnInteraction: false,
                slidesPerView: 'auto',
                paginationClickable: true,
                spaceBetween: 20
            })
        }
    }
});
function detailType(para) {
    var that = this.app;
    if (that.Type == 1) {
        para.title = "企业动态详情"
    }
    if (that.Type == 2) {
        para.title = "本地新闻详情"
    }
    if (that.Type == 3) {
        para.title = "协会动态详情"
    }
    if (that.Type == 4) {
        para.title = "政策动态详情"
    }
    if (that.Type == 5) {
        para.title = "人物详情"
    }
    if (that.Type == 6) {
        para.title = "访谈详情"
    }
    if (that.Type == 7) {
        para.title = "评析详情"
    }
}
